package com.xinqi.modules.course.chapter.controller.web;


import com.xinqi.common.base.response.R;
import com.xinqi.common.core.utils.MessageUtils;
import com.xinqi.modules.course.chapter.domain.CourseChapterEntity;
import com.xinqi.modules.course.chapter.dto.ChapterImportDTO;
import com.xinqi.modules.course.chapter.dto.ChapterSortDTO;
import com.xinqi.modules.course.chapter.dto.req.*;
import com.xinqi.modules.course.chapter.param.ChapterResponseDTO;
import com.xinqi.modules.course.chapter.dto.rsp.ChapterTreeDTO;
import com.xinqi.modules.course.chapter.service.CourseChapterService;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * web课程章节相关接口
 *
 * @author liuxingxing
 * @since 2023-03-08 10:10:45
 */
@Tag(name = "Web-课程章节", description = "课程章节")
@RestController
@RequestMapping("/web/courseChapter")
public class WebCourseChapterController {
    /**
     * 服务对象
     */
    @Autowired
    private CourseChapterService courseChapterService;

    /**
     * 初始化课程章节
     *
     * @param param 初始化参数
     * @return
     */
    @PostMapping("/initChapter")
    public R<?> initChapter(@Validated @RequestBody InitChapterDTO param) {
        courseChapterService.initChapterNew(param);
        return R.ok();
    }

    /**
     * 查询章节或者小节信
     *
     * @param queryParam 查询参数
     * @return 所有数据
     */
    @GetMapping("/selectChapterOrSonChapter")
    public R<List<ChapterResponseDTO>> selectChapterOrSonChapter(ChapterQueryDTO queryParam) {
        List<ChapterResponseDTO> courseChapterEntities = courseChapterService.selectChapterOrSonChapter(queryParam);
        return R.ok(courseChapterEntities);
    }

    /**
     * 新增数据
     *
     * @param courseChapter 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    public R<?> insert(@Validated @RequestBody CourseChapterEntity courseChapter) {
        this.courseChapterService.saveCourseChapter(courseChapter);
        return R.ok(courseChapter);
    }

    /**
     * 重命名
     *
     * @param updateChapterNameParam 更新参数
     * @return 修改结果
     */
    @PostMapping("/rename")
    public R<Boolean> rename(@Validated @RequestBody UpdateChapterNameDTO updateChapterNameParam) {
        CourseChapterEntity chapterEntity = new CourseChapterEntity();
        chapterEntity.setId(updateChapterNameParam.getChapterId());
        chapterEntity.setName(updateChapterNameParam.getNewName());
        return R.ok(this.courseChapterService.updateById(chapterEntity));
    }

    /**
     * 删除章节以及章节下面的内容(作业，讨论，互动课件等等)
     *
     * @param deleteChapterParam 主键结合
     * @return 删除结果
     */
    @PostMapping("/remove")
    public R<?> remove(@RequestBody DeleteChapterDTO deleteChapterParam) {
        List<Long> idList = deleteChapterParam.getIdList();
        if (idList != null && idList.size() > 0) {
            //删除章节
            this.courseChapterService.removeChapterAndContent(idList);
            return R.ok(MessageUtils.message("common.remove.success"));
        }
        return R.fail("参数非空");
    }

    /**
     * 章节排序
     *
     * @param sortParam 排序参数
     * @return 排序结果
     */
    @PostMapping("/sort")
    public R<?> sort(@Validated @RequestBody ChapterSortDTO sortParam) {
        this.courseChapterService.sort(sortParam);
        return R.ok();
    }

    /**
     * 章节树结构列表查询
     *
     * @param treeListQueryParam 查询参数
     * @return 结果
     */
    @PostMapping("/selectTreeList")
    public R<List<ChapterTreeDTO>> treeList(@Validated @RequestBody ChapterTreeListQueryDTO treeListQueryParam) {
        List<ChapterTreeDTO> vos = this.courseChapterService.treeList(treeListQueryParam);
        return R.ok(vos);
    }

    /**
     * 将一个班级的章节导入到另外一个班级
     *
     * @param importParam 导入参数
     * @return 结果
     */
    @PostMapping("/importChapter")
    public R<Boolean> importChapter(@RequestBody ChapterImportDTO importParam) {
        this.courseChapterService.importChapter(importParam);
        return R.ok();
    }

    /**
     * 统计章节和小节各有多少个
     *
     * @param classId 班级id
     * @return 结果
     */
    @GetMapping("/chapterCount/{classId}")
    public R chapterCount(@PathVariable("classId") Long classId) {
        Map<String, Object> res = this.courseChapterService.chapterCount(classId);
        return R.ok(res);
    }

}
